package com.bysj.sausys.controller.backend;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.beans.SubmitTaskBean;
import com.bysj.sausys.common.JsonData;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.model.SysAssAct;
import com.bysj.sausys.service.ISysAssActService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 社团活动申请Controller
 *
 * @author WangZK
 */
@Api(description = "社团活动申请接口")
@Controller
@RequestMapping("sys/assact")
public class SysAssActController {

    @Resource
    private ISysAssActService sysAssActService;

    /**
     * 新增社团活动申请
     *
     * @param sysAssAct
     * @param request
     * @return
     */
    @ApiOperation(value = "保存社团活动申请单接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonData save(@ApiParam(value = "社团活动申请单实体类", required = true) @RequestBody SysAssAct sysAssAct,
                         HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysAssActService.save(sysAssAct, UserConst.getCurrentUsername(map), UserConst.getCurrentAssId(map));
        return JsonData.success();
    }

    /**
     * 更新社团活动申请
     *
     * @param sysAssAct
     * @param request
     * @return
     */
    @ApiOperation(value = "修改社团活动申请单接口")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData update(@ApiParam(value = "社团活动申请单实体类", required = true) @RequestBody SysAssAct sysAssAct,
                           HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysAssActService.update(sysAssAct, UserConst.getCurrentUsername(map), UserConst.getCurrentAssId(map));
        return JsonData.success();
    }

    /**
     * 根据id获取社团活动申请
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据申请单id获取社团活动申请单信息")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getOne(@ApiParam(value = "社团活动申请单id", required = true) @RequestParam Integer id) {
        SysAssAct sysAssAct = sysAssActService.findById(id);
        return JsonData.success(sysAssAct);
    }

    /**
     * 分页获取活动申请
     *
     * @param pageNo
     * @param pageSize
     * @param request
     * @return
     */
    @ApiOperation(value = "获取社团活动申请单列表接口")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getPage(@ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") int pageNo,
                            @ApiParam(value = "每页显示条数") @RequestParam(required = false, defaultValue = "10") int pageSize,
                            HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        PageResult<SysAssAct> result = sysAssActService.getPage(new PageQuery(pageNo, pageSize), map);
        return JsonData.success(result);
    }

    /**
     * 删除活动申请
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除社团活动申请单接口")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonData delete(@ApiParam(value = "社团活动申请单id", required = true) @RequestParam Integer id) {
        sysAssActService.delete(id);
        return JsonData.success();
    }

    /**
     * 启动社团活动审批流程
     *
     * @param id
     * @param request
     * @return
     */
    @ApiOperation(value = "启动社团活动审批流程接口")
    @RequestMapping(value = "/startProcess", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData startProcess(@ApiParam(value = "社团活动申请单id", required = true) @RequestParam Integer id,
                                 HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysAssActService.saveStartProcess(id, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 获取审批信息
     *
     * @param taskId
     * @return
     */
    @ApiOperation(value = "获取审批信息接口")
    @RequestMapping(value = "/viewTaskFrom", method = RequestMethod.GET)
    @ResponseBody
    public JsonData viewTaskFrom(@ApiParam(value = "任务id", required = true) @RequestParam String taskId) {
        Map result = sysAssActService.getAuditInfo(taskId);
        return JsonData.success(result);
    }

    /**
     * 提交任务
     *
     * @param submitTaskBean
     * @param request
     * @return
     */
    @ApiOperation(value = "提交任务接口")
    @RequestMapping(value = "/submitTask", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData submitTask(@ApiParam(value = "任务提交封装类", required = true) @RequestBody SubmitTaskBean submitTaskBean,
                               HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysAssActService.saveSubmitTask(submitTaskBean, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 查看历史的审批信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查看历史的审批信息接口")
    @RequestMapping(value = "/viewHisComment", method = RequestMethod.GET)
    @ResponseBody
    public JsonData viewHisComment(@ApiParam(value = "社团活动申请单id", required = true) @RequestParam Integer id) {
        Map result = sysAssActService.getHisComment(id);
        return JsonData.success(result);
    }
}
